<template>
  <el-dialog id="edit-dialog" :title="$t('channel.ga.add')" :visible.sync="showState" :destroy-on-close="true" :before-close="handleClose" :close-on-click-modal="false" width="650px">
    <el-form ref="formValidate" :model="formValidate" :rules="rules" label-width="150px" class="demo-ruleForm">
      <el-form-item :label="$t('channel.tabData.companyName')" prop="company">
        <el-input v-model="formValidate.company" :placeholder="$t('channel.prompt.companyName')" :maxlength="30" />
      </el-form-item>
      <el-form-item :label="$t('channel.tabData.name')" prop="channelName">
        <el-input v-model="formValidate.channelName" :placeholder="$t('channel.prompt.name')" :maxlength="30" />
      </el-form-item>
      <el-form-item :label="$t('channel.ga.packageName')" prop="packageName">
        <el-select v-model="formValidate.packageName" :placeholder="$t('channel.prompt.packageName')" style="width: 100%">
          <el-option
            v-for="(item, index) in packageData"
            :key="index"
            :label="item"
            :value="item"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <p>{{ $t('channel.ga.tip') }}</p>
        <el-button type="primary" :disabled="!(formValidate.packageName && formValidate.channelName && formValidate.company)" @click="generate">{{ $t('channel.ga.generate') }}</el-button>
        <el-button @click="clear">{{ $t('channel.ga.clear') }}</el-button>
      </el-form-item>
      <el-form-item :label="$t('channel.ga.link')" prop="googleLink">
        <el-input
          v-model="formValidate.googleLink"
          type="textarea"
          :autosize="{ minRows: 4, maxRows: 6}"
          readonly
        />
        <el-button style="margin-top: 10px" :disabled="!formValidate.googleLink">{{ $t('common.copy') }}</el-button>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">{{ $t('common.operate.cancel') }}</el-button>
      <el-button type="primary" :loading="submitLoading" @click="handleSubmit">{{ $t('common.operate.sure') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addGaChannel } from '@/api/operation'
export default {
  name: 'ChannelGaDialog',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    packageData: {
      type: Array,
      default: function() {
        return []
      }
    }
  },
  data() {
    return {
      submitLoading: false,
      formValidate: {
        channelName: null,
        packageName: null,
        company: null,
        googleLink: null,
        shortLink: null
      },
      rules: {
        company: [{ required: true, message: this.$t('channel.tabData.companyName') + this.$t('common.null'), trigger: 'blur' }],
        packageName: [{ required: true, message: this.$t('channel.ga.packageName') + this.$t('common.null'), trigger: 'blur' }],
        channelName: [{ required: true, message: this.$t('channel.tabData.name') + this.$t('common.null'), trigger: 'blur' }],
        googleLink: [{ required: true, message: this.$t('channel.ga.link') + this.$t('common.null'), trigger: 'blur' }]
      }
    }
  },
  computed: {
    showState: {
      get: function() {
        return this.showDialog
      },
      set: function() {
      }
    }
  },
  methods: {
    handleClose() {
      this.$emit('change-dialog', false)
      this.clear()
    },
    handleSubmit() {
      this.$refs['formValidate'].validate((valid) => {
        if (valid) {
          const data = this.formValidate
          addGaChannel(data)
            .then(res => {
              this.submitLoading = false
              if (res.success) {
                this.$message.success(this.$t('common.tip.addSuccess'))
                this.$emit('edit-success')
                this.handleClose()
              } else {
                this.$message.error(res.message)
              }
            })
            .catch(err => {
              this.$message.error(err.message)
              this.submitLoading = false
            })
        }
      })
    },
    generate() {
      const data = this.formValidate
      this.formValidate.googleLink = 'https://play.google.com/store/apps/details?id=' + data['packageName'] + '&referrer=utm_source%3D' + data['channelName']
    },
    clear() {
      this.formValidate = {
        channelName: null,
        packageName: null,
        company: null,
        googleLink: null,
        shortLink: null
      }
    }
  }
}
</script>

<style scoped>

</style>
